<template>
  <el-select
      clearable
      filterable
      placeholder="请选择单位"
      class="ele-fluid"
      :value="value"
      @change="change">
    <el-option v-for="item in organization" :key="item.id" :label="item.name" :value=item.id />
  </el-select>
</template>

<script>
export default {
  props: {
    value: Number,
    model_type:String
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  data: function () {
    return {
      organization: [],
      loading: false,
    }
  },
  watch: {
    model_type() {
      this.getData()
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    change(val) {
      this.$emit('change', val)
    },
    getData() {
      this.loading = true
      this.$http.post('/unit/index?page=1&limit=999&model_type='+this.model_type).then(res => {
        if (res.data.code === 0) {
          this.organization = []
          for (let i in res.data.data) {
            this.organization.push({
              name: res.data.data[i]['unit_name'],
              id: res.data.data[i]['id']
            })
          }
        } else {
          this.$message.error(res.data.msg)
        }
        this.loading = false
      }).catch(e => {
        this.loading = false
        this.$message.error(e.message)
      })
    }
  },
};
</script>